package com.jic.weamop.controller;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.RestResult;
import com.jic.weamop.api.WeAMOPApi;
import com.jic.weamop.request.ChannelResponseApi;
import com.jic.weamop.request.MessageRequestApi;
import com.jic.weamop.weBankAMOP.common.dto.request.MessageRequest;
import com.jic.weamop.weBankAMOP.service.ContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.bcos.channel.dto.ChannelResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * Created by messigao on 2018/7/23.
 */
@RestController
@Slf4j
@RequestMapping("/rest")
@Api(tags = "AMOP 测试控制类")
public class WeAMOPFeign implements WeAMOPApi {

    @Autowired
    ContractService contractService;


    @ApiOperation(value = "发送消息接口")
    @PostMapping("/sendMessage")
    @Override
    public RestResult<ChannelResponseApi> sendMessage(@RequestBody MessageRequestApi messageRequestApi){
        ChannelResponseApi channelResponseApi = new ChannelResponseApi();
        ChannelResponse channelResponse = new ChannelResponse();
        RestResult restResult = new RestResult();
        try {
            MessageRequest request = new MessageRequest();
            BeanUtils.copyProperties(messageRequestApi,request);
            log.info("==========【weAMOP】消息服务接口，请求入参 = {}", JSON.toJSONString(request));
            channelResponse = contractService.getMessageService().sendMessage(request);
            log.info("==========【weAMOP】消息服务接口，请求出参 = {}", JSON.toJSONString(channelResponse));
            BeanUtils.copyProperties(channelResponse,channelResponseApi);
            restResult.setCode("0000");
            restResult.setMsg("success");
            restResult.setData(channelResponseApi);
        } catch (Exception e) {
            log.info("======================【weAMOP】消息服务系统异常 = {} ==================================",e.getMessage());
            restResult.setCode("9999");
            restResult.setMsg("error");
            restResult.setData(channelResponseApi);
        }

        return  restResult;
    }


    @ApiOperation(value = "发送消息接口测试")
    @PostMapping("/test2")
    @Override
    public String test(MessageRequestApi messageRequestApi) {
        log.info("==========【weAMOP】消息服务接口，请求入参 = {}", JSON.toJSONString(messageRequestApi));

        log.info("==========【weAMOP】消息服务接口，请求出参 = {}", JSON.toJSONString(messageRequestApi));
        return "ok";
    }

}
